<template>
  <div class="user-order">
    <van-nav-bar
      title="我的订单"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-tabs class="top-tabs">
      <van-tab title="全部"></van-tab>
      <van-tab title="待付款"></van-tab>
      <van-tab title="待使用"></van-tab>
      <van-tab title="待收货"></van-tab>
    </van-tabs>
    <!-- 购物车为空时的图片 -->
    <div v-show="orderList.length == 0 ? true : false">
      <img src="../assets/cartnull.png" alt="" />
      <h5 class="tips">空空如也，去首页看看吧~</h5>
    </div>
    <div class="order-item" v-for="item in orderList" :key="item.id">
      <h5>
        <span>收货人:{{ item.receiver }}</span>
        <span class="time"
          >下单时间：{{
            new Date(item.createdAt).toLocaleString().replace(/:\d{1,2}$/, " ")
          }}</span
        >
      </h5>
      <van-card
        v-for="detail in item.details.filter((item) => item.product)"
        :key="detail.id"
        :num="detail.amount"
        :price="detail.price.toFixed(2)"
        :title="detail.product.name"
        :thumb="detail.product.coverImage | dalImg"
      />
      <div class="bto">
        <h6>
          <p class="num">共{{ item.details.length }}件</p>
          <p class="total">
            合计:
            <span>￥{{ item.price.toFixed(2) }}</span>
          </p>
        </h6>

        <van-button
          type="warning"
          :to="{ name: 'OrderDetail', query: { id: item.id } }"
          >详情</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { loadOrdersAPI } from "../services/Orders";

export default {
  data() {
    return {
      orderList: [],
    };
  },
  async created() {
    const res = await loadOrdersAPI();
    // console.log(res);
    this.orderList = res.data;
    this.orderList = this.orderList.filter((item) => {
      if (item.details.filter((det) => det.product).length !== 0) {
        return item;
      }
    });
    // console.log(this.orderList);
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
  },
};
</script>

<style scoped>
.user-order h3 {
  text-align: center;
  background: #fff;
  padding: 0.8rem 0;
  font-size: 1rem;
}
.top-tabs {
  border-top: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  padding: 0.2rem 0;
  background: #fff;
}
.order-item {
  background: #fff;
  margin: 0.5rem 0;
}
.order-item h5,
.order-item .bto {
  border-bottom: 1px solid #ccc;
  border-top: 1px solid #ccc;
  padding: 0.5rem 0.8rem;
  font-size: 0.9rem;
  overflow: hidden;
}
.order-item .bto h6 {
  overflow: hidden;
  padding: 0.8rem;
  font-size: 0.9rem;
  font-weight: 400;
}
.order-item .bto .num {
  float: left;
}
.order-item .bto .total {
  float: right;
}
.order-item .bto .total span {
  color: deeppink;
  font-weight: bold;
}
.order-item .bto button {
  float: right;
  font-size: 1rem;
}
.order-item h5 .time {
  float: right;
}
.order-item p .total {
  float: right;
}
.tips {
  text-align: center;
  color: rgb(254, 90, 8, 0.8);
}
</style>